<template>
  <div>
    <el-dialog
      title="编辑用户"
      :visible.sync="visible"
      :modal-append-to-body="false"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      @close="closeDialog"
      
      width="500px"
    >
      <div class="warn">
        <p>
          <strong>说明:</strong>
          <br />标记*的必须录入数据的字段
        </p>
      </div>
      <div class="form"></div>
      <el-form ref="form" :model="submit" label-width="100px" size="mini">
        <el-form-item label="*登录名">
          <el-input readonly v-model="submit.username" class="small-width"></el-input>
        </el-form-item>
        <el-form-item label="*姓名">
          <el-input v-model="submit.name" class="small-width"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="submit.sex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="*所属组织">
          <el-input v-model="submit.orangename"></el-input>
          <el-popover placement="right" width="400" v-model="treeApp.visible">
            <div style="width:400px">
              <treeTable
                :tableData="treeApp.here_list.treelist"
                table_height="500"
                :firstColumn="treeApp.firstColumn"
                @rowclick="selectFather"
                v-if="treeApp.hackReset"
              >
                <el-table-column prop="code" label="编码" width="150"></el-table-column>
              </treeTable>
              <div style="width:100px;background:red;margin:3px auto;">
                <el-button type="primary" @click="SureSelectOrange">确定选择机构</el-button>
              </div>
            </div>
            <el-button slot="reference">选择</el-button>
          </el-popover>
        </el-form-item>
        <el-form-item label="入职日期">
          <el-col :span="11">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="submit.date"
              value-format="yyyy-MM-dd"
              style="width: 100%;"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="编码">
          <el-input v-model="submit.id" class="small-width"></el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="submit.cardId" class="small-width"></el-input>
        </el-form-item>
        <el-form-item label="现住地址">
          <el-input v-model="submit.address"></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="submit.phone"></el-input>
        </el-form-item>
        <el-form-item label="备用联系电话">
          <el-input v-model="submit.phone2"></el-input>
        </el-form-item>
        <el-form-item label="能否登录">
          <el-radio-group v-model="submit.statu">
            <el-radio label="允许登录"></el-radio>
            <el-radio label="禁止登录"></el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="Submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import treeTable from "../../../multiplexing/treetable";
export default {
  components: {
    treeTable
  },
  props: {
    form: {
      style: Object
    }
  },
  data() {
    return {
      submit: {
        password: "123456"
      },
      visible: true,
      treeApp: {
        hackReset: false,
        visible: false,
        here_list: {},
        firstColumn: {
          name: "机构组织名",
          prop: "name",
          width: "300"
        }
      }
    };
  },
  methods: {
    Check() {
      if (
        this.submit.username == null ||
        this.submit.username == "" ||
        this.submit.name == null ||
        this.submit.name == "" ||
        this.submit.orangeid == null ||
        this.submit.orangeid == ""
      ) {
        this.$message.error("请录入必填数据的字段！");
        return false;
      }
      return true;
    },
    Submit() {
      if (this.Check()) {
        this.$http
          .post("user/isAxist", { username: this.submit.username })
          .then(response => {
            if (response.data == false) {
              this.$message.error("该用户名不存在！");
            } else {
              this.$http.post("user/updata", this.submit).then(response => {
                if (response.data == "success") {
                  this.$message({
                    message: "修改完成",
                    type: "success"
                  });
                  this.NeedRefresh();
                  this.visible = false;
                }
              });
            }
          });
      }
    },
    closeDialog() {
      window.console.log("dfialog-close");
      this.$emit("dialog-close");
    },
    NeedRefresh() {
      this.$emit("refresh");
    },
    selectFather(node) {
      this.submit.orangeid = node.id;
      this.submit.orangename = node.name;
    },
    SureSelectOrange() {
      this.treeApp.visible = false;
    }
  },
  created() {
    this.$http.get("orangetree/list").then(response => {
      this.treeApp.here_list.treelist = response.data;
      this.$nextTick(() => {
        this.treeApp.hackReset = true;
      });
    });
    this.$http
      .post("orangetree/organename", { id: this.form.orangeid })
      .then(response => {
        this.submit.orangename = response.data;
      });
    this.submit = JSON.parse(JSON.stringify(this.form));
  }
};
</script>

<style>
.form {
  margin-top: 20px;
}
</style>